package com.njpi.stu.attendance.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.njpi.stu.attendance.entity.*;
import com.njpi.stu.attendance.entity.Class;
import com.njpi.stu.attendance.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 考勤表 前端控制器
 * </p>
 *
 * @author pxl
 * @since 2019-11-06
 */
@Controller
@RequestMapping("/check-work")
public class CheckWorkController {


    @Autowired
    private IDepartmentService departmentService;
    @Autowired
    private IClassService classService;
    @Autowired
    private ICourseService courseService;
    @Autowired
    private IUserService userService;
    @Autowired
    private ICheckWorkService checkWorkService;

    /**
     * 跳转发起签到页面
     * @param model
     * @param session
     * @return
     */
    @RequestMapping(method = RequestMethod.GET)
    public  String skipPage(Model model, HttpSession session){
        model.addAttribute("title","发起签到");

        //查找所有学院
        List<Department> depList = departmentService.list();
        model.addAttribute("depList",depList);
        //查找所有班级
        List<Class> classList = classService.list();
        model.addAttribute("classList",classList);
        //查找所有课程
        List<Course> courseList = courseService.list();
        model.addAttribute("courseList",courseList);
        //查找学院
        User user = (User) session.getAttribute("user");
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("class_name",user.getClassName());
        Class aClass = classService.getOne(queryWrapper);
        session.setAttribute("dep",aClass.getDepartmentName());

        return "checkWork";
    }


    /**
     *申请假条
     * @param checkWork
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/apply",method =RequestMethod.POST)
    public String saveLeaveNote(@RequestBody CheckWork checkWork){
        String name = SecurityContextHolder.getContext().getAuthentication().getName();
        checkWork.setUserId(Long.valueOf(name));
        checkWork.setState(1);
        checkWorkService.save(checkWork);

        return "已成功发起签到";
    }





}
